﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 智能仓库系统
{
    class Serial
    {
        public SerialPort serialPort1 = new SerialPort();
        public SerialPort serialPort2 = new SerialPort();
        public SerialPort serialPort3 = new SerialPort();
        public SerialPort serialPort4 = new SerialPort();
        public SerialPort serialPort5 = new SerialPort();
        public SerialPort serialPort6 = new SerialPort();

        public void Serial_Init()
        {           
            serialPort1.BaudRate = 9600;
            serialPort2.BaudRate = 9600;
            serialPort3.BaudRate = 9600;
            serialPort4.BaudRate = 9600;
            serialPort5.BaudRate = 9600;
            serialPort6.BaudRate = 9600;

            serialPort1.DataReceived += serialPort1_DataReceived;
            serialPort2.DataReceived += serialPort2_DataReceived;
            serialPort3.DataReceived += serialPort3_DataReceived;
            serialPort4.DataReceived += serialPort4_DataReceived;
            serialPort5.DataReceived += serialPort5_DataReceived;
            serialPort6.DataReceived += serialPort6_DataReceived;
        }

        public string[] SerialDataReceived = new string[6] { null, null, null, null, null, null };   //6个扫描枪对应的串口接收数据

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[1 - 1] = serialPort1.ReadTo("\r");
            SerialDataReceived[1 - 1] = SerialDataReceived[1 - 1].Replace("\n", "");
        }

        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[2 - 1] = serialPort2.ReadTo("\r");
        }

        private void serialPort3_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[3 - 1] = serialPort3.ReadTo("\r");
        }

        private void serialPort4_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[4 - 1] = serialPort4.ReadTo("\r");
        }

        private void serialPort5_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[5 - 1] = serialPort5.ReadTo("\r");
        }

        private void serialPort6_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialDataReceived[6 - 1] = serialPort6.ReadTo("\r");
        }
    }
}
